<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<html>
<head>
<title>设置cookie</title>
</head>
<body>
<%
String userid="123456";
Cookie cookie = new Cookie("ncme-userid", userid);
//这个要设置
cookie.setPath("/");
//这个也要设置才能实现上面的网站共用  cookie.setDomain("ncme.org.cn");  192.168.1.30
//cookie.setDomain("test.ncme.org.cn");
//cookie.setDomain("ncme.org.cn");
cookie.setDomain("localhost");
//不设置的话，则cookies不写入硬盘，而是写入在内存，只在当前页面有用，以秒为单位
cookie.setMaxAge(7*24*60*60);
response.addCookie(cookie);

%>
<div onclick="getCookie()"><button type="button"  >点击我显示cookie</button></div>

<div id="cooki">
</div>
<br/>
cookie值：<input type="text" id="cookieValue"/>
<div onclick="setCooki()"><button>JS设置coookie</button></div>
<br/>
<div onclick="delCookie('ncme-userid')"><button>JS删除coookie</button></div>
<br/>
<div><a href="getCookie.jsp">获取cookie页面</a></div>
<script type="text/javascript">
function getCookie(){
	var cookieStr=document.cookie;
	document.getElementById("cooki").innerHTML=cookieStr;
}

function setCooki(){
	var cookieValueStr=document.getElementById("cookieValue").value;
	setCookie('ncme-userid',cookieValueStr,5,'ncme.org.cn')
}

//设置cookie值，expiredays过期日期向后多少天.Domain域名
function setCookie(c_name,value,expiredays,Domain)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+";path=/;Domain="+Domain+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
alert("通过js设置cookie,"+c_name+":"+value);
}

function delCookie(name) {                   //删除一个cookie  
    var exp = new Date();  
    exp.setTime(exp.getTime() - 1);  
    var cval=getCookie(name);  
    if(cval!=null)  
    document.cookie= name + "="+cval+";expires="+exp.toUTCString();  
    getCookie();
}  
</script>
</body>
</html>